import commonConstants from "@/common/commonConstants";
import commonMethod from "@/common/commonMethod";
import $ from "jquery"

export default class TrackManager{

    constructor(v,configs,monitorM){
        this.curViewer = v;   

        //是否在想定推演中显示
        this.isTrackShow = false;
        this.trackMap = new Map();

        this.monitorManager = monitorM;
        this.trackMonitor = monitorM.getMonitorByKey(commonConstants.monitorKey.track);
        this.trackMonitor.setTrackManager(this);
        this.layerManager = undefined;

        this._handlerConfigs(configs);
    }

     //处理一些配置信息
     _handlerConfigs(configs){
        if (configs) {
             //没有就不处理
            configs.forEach(config => {
                if(config.getKey() == commonConstants.configurationKey.troop){
                    this.attributePlane = config.getValue();
                }
            });     
        }
    
    }

    setLayerManager(layerM){
        this.layerManager = layerM;
    }
 /**
     *
     * @return {null|(Track)[]}
     */
    getTracks(){
        let _this = this;
        return _this.trackMap.values();
        // let allTracks = [];
        // for (let trackId of _this.trackMap.keys()){
        //     allTracks.push(_this.trackMap.get(trackId));
        // }
        // return allTracks;
    }
    /**
     *
     * @param track {Track}
     */
    addTrack(track){
        let _this = this;
        if (!_this.trackMap.has(track.getTrackId())) {
            _this.trackMap.set(track.getTrackId(),track);
        }
    }

    removeTrackById(trackId){
        let _this = this;
        let trackById = _this.getTrackById(trackId);
        if (trackById){
            _this.removeTrack(trackById);
        }
    }
    /**
     * 删除航迹
     * @param track {Track}
     */
    removeTrack(track){
        let _this = this;
        if (_this.trackMap.has(track.getTrackId())) {
            _this.trackMap.delete(track.getTrackId());

            if (_this.layerManager){
                _this.layerManager.getTrackLayer().remove(track);
            }
            //清除地球中的航迹
            track.destroy(_this.curViewer);
        }
    }
    /**
     * 创建航迹
     * @param options
     * @return {Track}
     */
    createTrack(options){
        let _this = this;

        _this.trackMonitor.setParams(options);
        let positions = [];
        let positionsObj = options["trackPositions"];
        if (positionsObj){
            if ($.type(positionsObj).toLowerCase() === "string"){
                positions = commonMethod.recodingPositions(positionsObj);
            }else{
                positions = positionsObj;
            }
        }
        let track = _this.trackMonitor.createTrack(positions);
        _this.addTrack(track)

        if (_this.layerManager){
            _this.layerManager.getTrackLayer().addEntity(track);
        }
        return track;
    }
    /**
     *
     * @param trackId
     * @return {V|Track}
     */
    getTrackById(trackId){
        let _this = this;
        return _this.trackMap.get(trackId);
    }
    /**
     * 进入修改航迹
     * @param track {Track}
     */
    editTrack(track){
        let _this = this;
       
        track.setTrackControllerPointsSHow(true);
        _this.trackMonitor.setEdit(true);       
        _this.monitorManager.setCurMonitor(_this.trackMonitor);
    }
    drawTrack (options,trackCallback) {
        let _this = this;
     
        _this.trackMonitor.setParams(options);
        _this.trackMonitor.setTrackCallback(track=> {
            //创建好了后结束
            _this.addTrack(track);

            //图层管理
            if (_this.layerManager){
                _this.layerManager.getTrackLayer().addEntity(track)
            }

            if (trackCallback){
                trackCallback(track);
            }       
        });
        _this.monitorManager.deployMonitor(_this.trackMonitor);
    }
    setTrackAllShow(bool){
        let _this =this;
        //航迹显示
        for (let trackId of _this.trackMap.keys()){
            let track = _this.trackMap.get(trackId);
            track.setShow(bool);
        }
        _this.isTrackShow = bool;
    }
    dataClear(){
        let _this = this;
        _this.setTrackAllShow(false);
    }
    clear(){
        let _this = this;
        for (let trackId of _this.trackMap.keys()){
            let track = _this.trackMap.get(trackId);
            track.destroy(_this.curViewer);
        }
        _this.trackMap.clear();
    }
    reload(scenarioId) {
        let _this = this;
        // const loadResult = _this.dataStorage.load(CommonConstants.dataOperationType.track);
        // if (loadResult){
        //     let jsonTracks = loadResult["tracks"];
        //     for (let i = 0; i < jsonTracks.length; i++) {
        //         let jsonTrack = jsonTracks[i];
        //         _this.createTrack(jsonTrack["trackType"],jsonTrack);
        //     }
        // }
    }
}


export class Track{
    constructor(options){
        this.trackId = commonMethod.creatUUID();
        this.trackPositions = [];
        this.trackControllerPoints = [];

        this.trackName = "未命名";
        this.trackEntity = undefined;
        if (options){
            if (options["trackId"]){
                this.trackId = options["trackId"];
            }
            if (options["trackName"]){
                this.trackName = options["trackName"];
            }
            if (options["positions"]){
                this.trackPositions = options["positions"];
            }
            if (options["trackPositions"]){
                this.trackPositions = options["trackPositions"];
            }
            if (this.trackPositions){
                if ($.type(this.trackPositions).toLowerCase() === "string"){
                    _this.trackPositions = commonMethod.recodingPositions(this.trackPositions);
                }
            }
        }
    }
    getID(){
        let _this = this;
        return _this.trackId;
    }
    getTrackId(){
        let _this = this;
        return _this.trackId;
    }
    setTrackName(name){
        let _this = this;
        _this.trackName = name;
    }
    getEntity(){
        let _this =this;
        return _this.trackEntity;
    }
    setTrackEntity(trackEntity){
        let _this = this;
        _this.trackEntity = trackEntity;
    }
    setPositions(pos){
        let _this = this;
        _this.trackPositions = pos;
        if (this.trackPositions){
            if ($.type(this.trackPositions).toLowerCase() === "string"){
                _this.trackPositions = commonMethod.recodingPositions(this.trackPositions);
            }
        }
    }
    getPositions(){
        let _this = this;
        return _this.trackPositions;
    }
    getControlPoints(){
        let _this = this;
        return _this.trackControllerPoints;
    }
    addControlPoint(point){
        let _this = this;
        _this.trackControllerPoints.push(point);
    }
    changePosition(controlPoint,newPosition){
        let _this = this;
        if (controlPoint && newPosition){
            let index = _this.trackControllerPoints.indexOf(controlPoint);
            if (index !== -1){
                controlPoint.position.setValue(newPosition);
                _this.trackPositions[index] = newPosition;
            }
        }
    }
    setShow(bool){
        let _this = this;
        if (_this.trackEntity){
            _this.trackEntity.show = bool;
        }
    }
    setTrackControllerPointsSHow(bool){
        const _this = this;
        for (let i = 0; i < _this.trackControllerPoints.length; i++) {
            _this.trackControllerPoints[i].show = bool;
        }
    }
    prepare(){
        let _this =this;
        console.log("航迹:"+_this.trackName+"---->准备就绪");
    }
    gc(){
        let _this =this;
        console.log("航迹:"+_this.trackName+"---->回收完成");
    }
    destroy(v) {
        let _this =this;
        v.entities.remove(_this.trackEntity);
        for (let i = 0; i < _this.trackControllerPoints.length; i++) {
            v.entities.remove(_this.trackControllerPoints[i]);
        }
        // _this.trackPositions = [];
        // _this.trackControllerPoints = [];
    }
    toJson() {
        let _this = this;
        return {
            trackId:_this.trackId,
            trackName:_this.trackName,
            trackPositions:commonMethod.toJsonForPositions(_this.trackPositions)
        }
    }
}